#include "stm32f10x.h"                  // Device header
#include "MyFlash.h"

/**
  * @brief  Read one-word data from Flash
	*
  * @param  Address : the Address of FLASH
  * @retval one-word data from Flash
  */
uint32_t MyFLASH_ReadWord(uint32_t Address)
{
	return *((__IO uint32_t *)(Address));
}


/**
  * @brief  Read half-word data from Flash
	*
  * @param  Address : the Address of FLASH
  * @retval half-word data from Flash
  */
uint16_t MyFLASH_ReadHalfWord(uint32_t Address)
{
	return *((__IO uint16_t *)(Address));
}


/**
  * @brief  Read byte data from Flash
	*
  * @param  Address : the Address of FLASH
  * @retval byte data from Flash
  */
uint8_t MyFLASH_ReadByte(uint32_t Address)
{
	return *((__IO uint8_t *)(Address));
}


/**
  * @brief  Erase All Data in The inner-flash
	*
  */
void MyFLASH_EraseAllPages(void)
{
	// 1. 解锁
	FLASH_Unlock();
	
	// 2. 擦除所有页面
	FLASH_EraseAllPages();

  // 3. 锁 Flash
	FLASH_Lock();
}


/**
  * @brief  Read byte data from Flash
	*
  * @param  PageAddress : the Address of page start
  */
void MyFLASH_ErasePage(uint32_t PageAddress)
{
	// 1. 解锁
	FLASH_Unlock();
	
	// 2. 擦除所有页面
	FLASH_ErasePage(PageAddress);

  // 3. 锁 Flash
	FLASH_Lock();
}


/**
  * @brief  Write One-Word Data to Flash at PageAddress
	*
  * @param  Address : The address to which data needs to be written
  * @param  Data    : ...
  */
void MyFLASH_ProgramWord(uint32_t Address, uint32_t Data)
{
	// 1. 解锁
	FLASH_Unlock();
	
	// 2. 在指定页面写入一个字
	FLASH_ProgramWord(Address, Data);

  // 3. 锁 Flash
	FLASH_Lock();
}


/**
  * @brief  Write Half-Word Data to Flash at PageAddress
	*
  * @param  Address : The address to which data needs to be written
  * @param  Data    : ...
  */
void MyFLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
{
	// 1. 解锁
	FLASH_Unlock();
	
	// 2. 在指定页面写入一个字
	FLASH_ProgramHalfWord(Address, Data);

  // 3. 锁 Flash
	FLASH_Lock();
}